package com.tianze.tzkf.common;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import java.util.Stack;

/**
 * Created by parry on 2017.03.22
 *
 */
public class ActivityManager {


    private ActivityManager(){

    }
    private static ActivityManager activityManager = new ActivityManager();

    public static ActivityManager getInstance(){
        return activityManager;
    }


    private Stack<Activity> activityStack = new Stack<>();


    /**
     * 添加Activity到堆栈
     */
    public void add(Activity activity){
        if(activity != null){
            activityStack.add(activity);
        }
    }


    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    public Activity currentActivity(){
        Activity activity= activityStack.lastElement();
        return activity;
    }
    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity(){
        Activity activity=activityStack.lastElement();
        if(activity!=null){
            activity.finish();
            activity=null;
        }
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivitys(Activity activity){
        if(activity!=null){
            activityStack.remove(activity);
            activity.finish();
            activity=null;
        }
    }
    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls){
        for (Activity activity : activityStack) {
            if(activity.getClass().equals(cls) ){
                finishActivity(activity);
            }
        }
    }
    /**
     * 结束所有Activity
     */
    public void finishAllActivity(){
        for (int i = 0, size = activityStack.size(); i < size; i++){
            if (i == 0){

            }else {
                if (null != activityStack.get(i)) {
                    activityStack.get(i).finish();
                }
            }
        }
        activityStack.clear();
    }

    /**
     * 查询Activity个数
     *
     */
    public void checkActivityNum(){
        Log.e("checkActivityNum",activityStack.size()+"个");
    }

//    /**
//     * 退出应用程序
//     */
//    public void AppExit(Context context) {
//        try {
//            finishAllActivity();
//            ActivityManager activityMgr= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//            activityMgr.restartPackage(context.getPackageName());
//            System.exit(0);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

























    public void remove(Activity activity){
        if(activity != null){
            for(int i = activityStack.size() - 1;i >= 0;i--){
                Activity currentActivity = activityStack.get(i);
                if(currentActivity.getClass().equals(activity.getClass())){
                    currentActivity.finish();
                    activityStack.remove(i);
                }
            }
        }
    }


//    public void removeCurrent(){
//        Activity activity = activityStack.lastElement();
//        activity.finish();
//        activityStack.remove(activity);
//    }


//    public void removeAll(){
//        for (int i = activityStack.size() - 1;i >= 0;i--){
//            Activity activity = activityStack.get(i);
//            activity.finish();
//            activityStack.remove(activity);
//        }
//    }


    public int size(){
        return activityStack.size();
    }
    /**
     * 结束指定类名的Activity
     */
    public void finishActivityclass(Class<?> cls) {
        if (activityStack != null) {
            for (Activity activity : activityStack) {
                if (activity.getClass().equals(cls)) {
                    this.activityStack.remove(activity);
                    finishActivity(activity);
                    break;
                }
            }
        }

    }
    /**
     * 结束指定的Activity
     *
     * @param activity
     */

    public void finishActivity(Activity activity) {

        if (activity != null) {
            this.activityStack.remove(activity);
            activity.finish();
        }
    }

}
